package cn.xz.mxxz.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import cn.xz.mxxz.R
import cn.xz.mxxz.adapter.NetListAdapter
import cn.xz.mxxz.adapter.SearchListAdapter
import cn.xz.mxxz.base.BaseActivity
import cn.xz.mxxz.base.BaseApplication
import cn.xz.mxxz.bean.*
import cn.xz.mxxz.databinding.ActivityDreamBinding
import cn.xz.mxxz.databinding.ActivityNetBinding
import cn.xz.mxxz.net.ControlDevice
import cn.xz.mxxz.net.OkHttpUtil
import cn.xz.mxxz.net.OkhttpResponseHandle
import cn.xz.mxxz.util.GetListUtils
import cn.xz.mxxz.util.ToastUtil

/**
 * 互联网党建-三破三立*/
class NetActivity : BaseActivity<ActivityNetBinding>(R.layout.activity_net) {

    //标识符
    private var mTag = 0
    //设备信息
    private var mDevice: DeviceList? = null
    //当前列表所有数据
    private var mRows: ArrayList<RowsData> = arrayListOf()
    //当前界面请求标识
    private val mwsType = "dq_dj_web"

    override fun initData() {
        //获取当前设备信息
        mTag = intent.getIntExtra(CommonLib.JUMP_TYPE, -1)
        if (mTag != -1){
            mDevice = ControlDevice.getThisDevice(mTag)
            if (CommonLib.MY_LOCAL_IP.isNotEmpty() && mDevice != null){
                mDevice!!.ip = CommonLib.MY_LOCAL_IP
            }
            mDevice!!.ip = CommonLib.WEB_IP
            mDevice!!.port = CommonLib.WEB_PORT
        }
        //如果数据异常直接退出
        if (mTag == -1 || mDevice == null){
            ToastUtil.show("当前数据异常")
            finish()
        }else{
            //设置标题
            mDataBind.tvTitle.text = ControlDevice.getDeviceName(mTag)
            //获取数据
            initNet()
        }
    }

    override fun initEvent() {
        //返回
        mDataBind.tvBack.setOnClickListener {
            finish()
        }
        //上一张
        mDataBind.tvS.setOnClickListener {
            OkHttpUtil.postUrlWebType(mDevice!!.ip,mDevice!!.port,"albumControl",1,mwsType)
        }
        //下一张
        mDataBind.tvX.setOnClickListener {
            OkHttpUtil.postUrlWebType(mDevice!!.ip,mDevice!!.port,"albumControl",2,mwsType)
        }
        //返回数据页
        mDataBind.tvD.setOnClickListener {
            OkHttpUtil.postUrlWebID(mDevice!!.ip,mDevice!!.port,"redirectPage",0,mwsType)
        }
    }

    private fun initNet(){
        val mUrl = "http://${mDevice!!.ip}:${mDevice!!.port}/business_dj/app/cockpit/getInfo"
        OkHttpUtil.get(this, mUrl,
            CockpitBean::class.java, object : OkhttpResponseHandle<CockpitBean?>() {
                override fun onSuccess(response: CockpitBean?) {
                     if (response != null && response.rows.isNotEmpty()){
                         mDataBind.list1.post {
                             mRows.addAll(response.rows)
                             setType1() //一次性全部显示
                         }
                     }
                }
            })
    }


    //设置一级标题
    private fun setType1(){
        if (mRows.isNotEmpty()){
            val mList1: ArrayList<NetListBean> = arrayListOf()
            mRows.forEach {
                if (it.parentId == 0){
                    //一级
                    mList1.add(getTestData(it))
                }
            }
            setList1(mList1)

            //二级所有内容
            val mList2: ArrayList<NetListBean> = arrayListOf()
            mList1.forEach { m1->
                if (mRows.isNotEmpty()){
                    mRows.forEach {
                        if (it.parentId == m1.Tag){
                            //二级
                            mList2.add(getTestData(it))
                        }
                    }
                }
            }
            setList2(mList2)


            //三级所有内容
            val mList3: ArrayList<NetListBean> = arrayListOf()
            mList2.forEachIndexed { index, netListBean ->
                if (mRows.isNotEmpty()){
                    if (index != 0){
                        mList3.add(NetListBean("",-999,-1,true,false))
                    }
                    mRows.forEach {
                        if (it.parentId == netListBean.Tag){
                            //三级
                            mList3.add(getTestData(it))
                        }
                    }
                }
            }
            setList3(mList3)

        }
    }

    //一级列表
    private val mAdapter1 = NetListAdapter()
    private fun setList1(mData: ArrayList<NetListBean>){
        val ms = LinearLayoutManager(BaseApplication.getContext())
        ms.orientation = LinearLayoutManager.VERTICAL
        mDataBind.list1.layoutManager = ms
        mAdapter1.setOnItemClickListener { adapter, view, position ->
            clearList()
            mData[position].select = true
            mAdapter1.notifyDataSetChanged()
            OkHttpUtil.postUrlWebID(mDevice!!.ip,mDevice!!.port,"redirectPage",mData[position].Tag!!,mwsType)
        }
        mDataBind.list1.adapter = mAdapter1
        mAdapter1.setList(mData)
    }

    //二级列表
    private val mAdapter2 = NetListAdapter()
    private fun setList2(mData: ArrayList<NetListBean>){
        val ms = LinearLayoutManager(BaseApplication.getContext())
        ms.orientation = LinearLayoutManager.VERTICAL
        mDataBind.list2.layoutManager = ms
        mAdapter2.setOnItemClickListener { adapter, view, position ->
            clearList()
            mData[position].select = true
            mAdapter2.notifyDataSetChanged()
            OkHttpUtil.postUrlWebID(mDevice!!.ip,mDevice!!.port,"redirectPage",mData[position].Tag!!,mwsType)
        }
        mDataBind.list2.adapter = mAdapter2
        mAdapter2.setList(mData)
    }

    //三级列表
    private val mAdapter3 = NetListAdapter()
    private fun setList3(mData: ArrayList<NetListBean>){
        val ms = LinearLayoutManager(BaseApplication.getContext())
        ms.orientation = LinearLayoutManager.VERTICAL
        mDataBind.list3.layoutManager = ms
        mAdapter3.setOnItemClickListener { adapter, view, position ->
            if (mData[position].Tag!! != -999){
                clearList()
                mData[position].select = true
                mAdapter3.notifyDataSetChanged()
                OkHttpUtil.postUrlWebID(mDevice!!.ip,mDevice!!.port,"redirectPage",mData[position].Tag!!,mwsType)
            }
        }
        mDataBind.list3.adapter = mAdapter3
        mAdapter3.setList(mData)
    }

    //三级列表选择都清空
    private fun clearList(){
        val mLi1 = mAdapter1.data as ArrayList<NetListBean>
        mLi1.forEach {
            it.select = false
        }
        mAdapter1.notifyDataSetChanged()

        val mLi2 = mAdapter2.data as ArrayList<NetListBean>
        mLi2.forEach {
            it.select = false
        }
        mAdapter2.notifyDataSetChanged()

        val mLi3 = mAdapter3.data as ArrayList<NetListBean>
        mLi3.forEach {
            it.select = false
        }
        mAdapter3.notifyDataSetChanged()
    }


    private fun getTestData(rew: RowsData): NetListBean{
        return NetListBean(Name = rew.name, Tag = rew.id, ParentTag = rew.parentId)
    }

}